import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/const/color.dart';
import 'package:lx_widgets_library/const/constant.dart';
import 'package:lx_widgets_library/const/font_weight.dart';
import 'package:lx_widgets_library/r.dart';

///
/// 导航栏控制器
/// 标题栏+内容+底部悬浮按钮
///
class BaseNavigationView extends StatelessWidget {
  // 标题
  final String? title;
  // 标题文字颜色
  final Color? titleColor;
  // 标题组件
  final Widget? titleView;

  // 导航栏背景色
  final Color? barBackgroundColor;
  // 导航栏背景是否透明
  final bool? barTransparent;
  // 当透明度为0时，是否隐藏导航栏
  final bool? hideBarWhenOpacity;
  // 是否隐藏导航栏阴影
  final bool? hideNavShadow;
  // 透明度(可变)
  final RxDouble? barOpacity;
  // 内容上移置顶（标题栏透明时用到）
  final bool? extendBodyBehindBar;

  // 控制器背景色
  final Color? backgroundColor;
  // 页面主体
  final Widget? body;

  // 隐藏返回按钮
  final bool? hidenBack;
  // 返回按钮
  final Widget? backView;
  // 返回按钮颜色
  final Color? navBackViewColor;

  // 右侧按钮
  final List<Widget>? rightViews;

  // 点击空白处隐藏键盘
  final bool? hiddenKeyboardWhenTouch;

  // 悬浮按钮
  final Widget? floatingActionButton;
  // 悬浮按钮位置
  final FloatingActionButtonLocation? floatingActionButtonLocation;
  // 悬浮按钮动画
  final FloatingActionButtonAnimator? floatingActionButtonAnimator;

  // 这个小部件出现在appBar的底部
  final PreferredSizeWidget? bottomView;
  // 页面底部组建
  final Widget? bottomNavigationBar;

  /// 在 Flutter 中 Scaffold 默认情况下 resizeToAvoidBottomInset 为 true，
  /// 当 resizeToAvoidBottomInset 为 true 时，Scaffold 内部会将 mediaQuery.viewInsets.bottom 参与到 BoxConstraints 的大小计算，也就是键盘弹起时调整了内部的 bottom 位置来迎合键盘。
  final bool? resizeToAvoidBottomInset;

  // 是否深色模式
  final bool? isDarkMode;

  const BaseNavigationView({
    required this.body,
    required this.barOpacity,
    Key? key,
    this.titleView,
    this.title,
    this.backgroundColor,
    this.titleColor,
    this.barBackgroundColor,
    this.navBackViewColor,
    this.barTransparent,
    this.extendBodyBehindBar,
    this.backView,
    this.rightViews,
    this.hiddenKeyboardWhenTouch,
    this.hidenBack,
    this.hideNavShadow,
    this.resizeToAvoidBottomInset,
    this.floatingActionButton,
    this.floatingActionButtonLocation,
    this.floatingActionButtonAnimator,
    this.bottomView,
    this.bottomNavigationBar,
    this.hideBarWhenOpacity,
    this.isDarkMode,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => Scaffold(
        appBar: (barOpacity!.value == 0 && hideBarWhenOpacity == true)
            ? null
            : _buildNavigationBar(),
        // 右滑菜单栏
        // endDrawer: endDrawerWidget,
        // 标题栏透明后，内容上移置顶
        extendBodyBehindAppBar: extendBodyBehindBar ??
            barTransparent == true || barOpacity!.value < 1,
        // view
        body: _buildBody(context),
        resizeToAvoidBottomInset: resizeToAvoidBottomInset ?? true,
        backgroundColor: backgroundColor,
        floatingActionButton: floatingActionButton,
        floatingActionButtonLocation: floatingActionButtonLocation,
        floatingActionButtonAnimator: floatingActionButtonAnimator,
        bottomNavigationBar: bottomNavigationBar,
      ),
    );
  }

  // 导航栏
  PreferredSizeWidget _buildNavigationBar() {
    return AppBar(
      title: Opacity(
        opacity: barOpacity?.value ?? 1.0,
        child: titleView ??
            Text(
              title ?? '',
              style: TextStyle(
                color: titleColor ?? WhiteColor.white1,
                fontSize: LxFontWeightEnum.titleLarge.size,
                fontWeight: LxFontWeightEnum.titleLarge.fontWeight,
              ),
            ),
      ),
      // 设置导航栏背景色
      // forceMaterialTransparency: true,
      surfaceTintColor: Colors.transparent, // 禁用表面色调
      scrolledUnderElevation: 0, // 禁用滚动时的高度变化
      flexibleSpace: Container(
        // 在这里设置想要的背景色
        color: barTransparent == true
            ? Colors.transparent
            : (barBackgroundColor ?? Colors.black).withAlpha(
                (barOpacity!.value * 255).truncate(),
              ),
      ),
      // 设置阴影辐射范围
      elevation: hideNavShadow == true ? 0 : 6.0.w,
      shadowColor:
          hideNavShadow == true ? Colors.transparent : const Color(0x1E8D8D8D),
      centerTitle: true,
      titleSpacing: 0,
      // 隐藏返回按钮时，leading需要设置为null
      automaticallyImplyLeading: hidenBack == true ? false : true,
      leading: hidenBack == true
          ? null
          : backView ??
              InkWell(
                onTap: () => Get.back(),
                child: SizedBox(
                  width: navigationBarHeight,
                  height: navigationBarHeight,
                  child: Image.asset(
                    LxR.nav_back_png,
                    package: PACKAGE_NAME,
                    width: 24.w,
                    height: 24.w,
                  ),
                ),
              ),
      actions: rightViews,
      // 修改返回按钮颜色
      iconTheme: IconThemeData(color: navBackViewColor ?? Colors.white),
      systemOverlayStyle: isDarkMode ?? Get.isDarkMode
          ? const SystemUiOverlayStyle(
              statusBarColor: Colors.transparent,
              systemNavigationBarIconBrightness: Brightness.light,
              statusBarIconBrightness: Brightness.light,
              statusBarBrightness: Brightness.dark)
          : const SystemUiOverlayStyle(
              statusBarColor: Colors.transparent,
              systemNavigationBarIconBrightness: Brightness.light,
              statusBarIconBrightness: Brightness.dark,
              statusBarBrightness: Brightness.light,
            ),
      toolbarHeight: 50.h,
      bottom: bottomView,
    );
  }

  // 主页面
  _buildBody(BuildContext context) {
    return hiddenKeyboardWhenTouch == true
        ? GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: () {
              // 点击空白处隐藏键盘
              FocusScopeNode currentFocus = FocusScope.of(context);
              if (!currentFocus.hasPrimaryFocus &&
                  currentFocus.focusedChild != null) {
                currentFocus.requestFocus(FocusNode());
              }
            },
            child: body,
          )
        : body;
  }
}
